package com.yikaiye.android.yikaiye.ui.activity.login;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivityRegisterBinding;
import com.yikaiye.android.yikaiye.ui.activity.login.viewmodel.RegisterViewModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding, RegisterViewModel> {

    private boolean isShow;

    @Override
    protected RegisterViewModel bindModel() {
        return getViewModel(this, RegisterViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        binding.setRegisterViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.back, o -> finish());
        viewModel.onDelayClick(binding.cdtCountdown, o -> {
            if (!TextUtils.isEmpty(viewModel.phoneNumber.getValue())) {
                showLoadingDialog();
                viewModel.sendCode(0, viewModel.phoneNumber.getValue());
            } else {
                ToastHelper.show(getString(R.string.please_input_cell_number));
            }
        });
        viewModel.onDelayClick(binding.ivPwdStatus, o -> {
            isShow = !isShow;
            binding.etPassword.setTransformationMethod(isShow ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            binding.ivPwdStatus.setImageResource(isShow ? R.mipmap.icon_pass_open : R.mipmap.icon_pass_close);
            binding.etPassword.setSelection(binding.etPassword.getText().toString().length());
        });

        viewModel.onDelayClick(binding.tvLogin, o -> finish());

        viewModel.onDelayClick(binding.btnRegister, o -> {
            if (!CommonUtil.checkPassword(viewModel.password.getValue())) {
                ToastHelper.show(getString(R.string.input_password));
                return;
            }
            showLoadingDialog();
            viewModel.register(viewModel.phoneNumber.getValue(), viewModel.code.getValue(), viewModel.password.getValue());
        });
        viewModel.phoneNumber.observe(this, s -> changeBtnStatus());
        viewModel.code.observe(this, s -> changeBtnStatus());
        viewModel.password.observe(this, s -> changeBtnStatus());

        // 获取验证码成功
        viewModel.codeSuccess.observe(this, verifyCodeModel -> {
            dismissLoadingDialog();
            binding.cdtCountdown.startCountDown(
                    context,
                    60,
                    getString(R.string.send_code_again),
                    "s"
            );
            ToastHelper.show(getString(R.string.code_has_been_sent));
        });
        // 获取验证码失败
        viewModel.codeError.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(s);
        });

        // 注册成功
        viewModel.registerSuccess.observe(this, userModel -> {
            finish();
            dismissLoadingDialog();
        });
        // 注册失败
        viewModel.registerError.observe(this, s -> {
            ToastHelper.show(s);
            dismissLoadingDialog();
        });
    }

    private void changeBtnStatus() {
        if (!TextUtils.isEmpty(viewModel.phoneNumber.getValue())
                && !TextUtils.isEmpty(viewModel.code.getValue())
                && !TextUtils.isEmpty(viewModel.password.getValue())) {
            binding.btnRegister.setEnabled(true);
            binding.btnRegister.setBackgroundResource(R.drawable.shape_f93f50_4dp);
        } else {
            binding.btnRegister.setEnabled(false);
            binding.btnRegister.setBackgroundResource(R.drawable.shape_fcebea_4dp);
        }
    }
}